package com.webchat.client.payment.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.CreateGroupRequestVO;
import com.webchat.domain.vo.response.AccountRelationResponseVO;
import com.webchat.domain.vo.response.WaitConfirmUserResponseVO;
import com.webchat.rmi.user.AccountRelationClient;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountRelationService {


    @Autowired
    private AccountRelationClient accountRelationClient;


    /**
     * 订阅/关注
     *
     * @param userId
     * @param account
     * @return
     */
    public Boolean subscribe(String userId, String account) {
        APIResponseBean<Boolean> responseBean = accountRelationClient.subscribe(userId, account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 取消订阅/关注
     *
     * @param account
     * @return
     */
    public Boolean unsubscribe(String userId, String account) {
        APIResponseBean<Boolean> responseBean = accountRelationClient.unsubscribe(userId, account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 确认好友
     *
     * @param userId
     * @param relationId
     * @return
     */
    public Boolean confirmSubscribe(String userId, Long relationId) {
        APIResponseBean<Boolean> responseBean = accountRelationClient.confirmSubscribe(userId, relationId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 拒绝好友
     *
     * @param relationId
     * @return
     */
    public Boolean rejectSubscribe(String userId, Long relationId) {
        APIResponseBean<Boolean> responseBean = accountRelationClient.rejectSubscribe(userId, relationId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return ObjectUtils.equals(responseBean.getData(), true);
        }
        throw new BusinessException(responseBean.getMsg());
    }


    /**
     * 加载用户好友/群组/机器人/公众号列表
     *
     * @param userId
     * @return
     */
    public AccountRelationResponseVO loadAccountRelations(String userId) {
        APIResponseBean<AccountRelationResponseVO> responseBean = accountRelationClient.listRelations(userId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 查询用户待审核用户列表
     *
     * @param account
     * @return
     */
    public List<WaitConfirmUserResponseVO> listWaitConformUsers(String account) {

        APIResponseBean<List<WaitConfirmUserResponseVO>> responseBean = accountRelationClient.listWaitConformUsers(account);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        // RPC远程调用异常 TODO，需要预警，上报异常到Prometheus
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 创建群聊
     *
     * @param createGroupRequest
     * @return
     */
    public String createChatGroup(CreateGroupRequestVO createGroupRequest) {
        APIResponseBean<String> responseBean = accountRelationClient.createChatGroup(createGroupRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }
}
